﻿using AssemblyExtensions = MicroCloud.Utils.AssemblyExtensions;

namespace MicroCloud.Api.Systems.Controllers
{
    /// <summary>
    /// 公共接口
    /// </summary>
    [AllowAnonymous]
    [Description("公共接口")]
    public class CommonController : SystemsControllerBase
    {
        #region "属性"
        #region "获取 验证码处理功能"
        /// <summary>
        /// 获取 验证码处理功能
        /// </summary>
        protected IVerifyCodeService VerifyCodeService => ServiceProvider.GetService<IVerifyCodeService>();
        #endregion

        #endregion

        #region "Action方法"
        #region "获取验证码图片"
        /// <summary>
        /// 获取验证码图片
        /// </summary>
        /// <returns>验证码图片文件</returns>
        [HttpGet]
        [NonFunction]
        [Description("验证码")]
        public virtual VerifyCodeDto VerifyCode()
        {
            ValidateCoder coder = new()
            {
                RandomColor = true,
                RandomItalic = true,
                RandomLineCount = 7,
                RandomPointPercent = 10,
                RandomPosition = true
            };
            var image = coder.CreateImage(4, out string code);
            string id = VerifyCodeService.SetCode(code);
            VerifyCodeDto dto = new()
            {
                Id = id,
                Image = image.ToBase64String(),
                Code = "",
            };
            return dto;
        }
        #endregion
        #region "校验验证码，只作为前端Ajax验证，验证成功不移除验证码，验证码仍需传到后端进行再次验证"
        /// <summary>
        /// 校验验证码
        /// <para>只作为前端Ajax验证，验证成功不移除验证码，验证码仍需传到后端进行再次验证</para>
        /// </summary>
        /// <param name="code">验证码字符串</param>
        /// <param name="id">验证码编号</param>
        /// <returns>是否无效</returns>
        [HttpGet]
        [NonFunction]
        [Description("校验验证码")]
        public virtual bool CheckVerifyCode(string code, string id)
        {
            if (code.IsMissing() || id.IsMissing())
            {
                return true;
            }
            return VerifyCodeService.CheckCode(code, id, false);
        }
        #endregion

        #region "获取系统信息"
        /// <summary>
        /// 获取系统信息
        /// </summary>
        /// <returns>系统信息</returns>
        [HttpGet]
        [NonFunction]
        [Description("系统信息")]
        public virtual object SystemInfo()
        {
            IServiceProvider provider = HttpContext.RequestServices;

            dynamic info = new ExpandoObject();
            info.Packs = provider.GetAllPacks().Select(m => new
            {
                m.GetType().Name,
                Class = m.GetType().FullName,
                m.Level,
                LevelDescription = m.Level.ToString(),
                m.OrderNo,
                m.IsEnabled
            }).ToList();

            string cliVersion = AssemblyExtensions.GetCliVersion();
            //string productVersion = Assembly.GetExecutingAssembly().GetProductVersion();
            string productVersion = Assembly.GetEntryAssembly().GetProductVersion();

            info.Object = new
            {
                Message = I18N.T("数据服务已启动"),
                CliVersion = cliVersion,
                ProductVersion = productVersion
            };

            return info;
        }
        #endregion
        #region "获取第三方登录信息"
        /// <summary>
        /// 获取第三方登录信息
        /// </summary>
        /// <returns>第三方登录信息</returns>
        [HttpGet]
        [NonFunction]
        [Description("第三方登录信息")]
        public virtual object OAuth2Info()
        {
            Dictionary<string, bool> info = new();

            IServiceProvider provider = HttpContext.RequestServices;

            IDictionary<string, OAuth2Option> dict = provider.GetMicroCloudOptions()?.OAuth2;
            if (dict == null)
            {
                return info;
            }
            foreach (var (name, options) in dict)
            {
                info.Add(name, options.Enabled);
            }

            return info;
        }
        #endregion

        #endregion

    }

}
